# Copyright 1999-2013 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2


#based on the old davmail-bin ebuild 

EAPI=7

EANT_BUILD_TARGET="dist"
EANT_NEEDS_TOOLS="true"
JAVA_ANT_REWRITE_CLASSPATH="true"

inherit eutils java-pkg-2 java-ant-2 user

DESCRIPTION="DavMail POP/IMAP/SMTP/Caldav/Carddav/LDAP Exchange and Office 365 Gateway"
HOMEPAGE="http://davmail.sourceforge.net/"
SRC_URI="https://github.com/mguessan/davmail/archive/${PV}.tar.gz -> ${P}.tar.gz"

LICENSE="GPL-2"
SLOT="0"
IUSE="server"
KEYWORDS="~amd64 ~x86"

EANT_GENTOO_CLASSPATH=""

#todo: make packages for all of them....
EANT_GENTOO_CLASSPATH_EXTRA=":dist/lib/woodstox-core.jar:dist/lib/commons-collections.jar:dist/lib/log4j.jar:dist/lib/javax.mail.jar:dist/lib/commons-logging:dist/lib/stax2-api.jar:dist/lib/jettison.jar:dist/lib/jdom.jar:dist/lib/slf4j-log4j12.jar:dist/lib/httpcore.jar:dist/lib/davmail.jar:dist/lib/libgrowl.jar:dist/lib/jackrabbit-webdav.jar:dist/lib/slf4j-api.jar:dist/lib/jcharset.jar:dist/lib/jcifs.jar:dist/lib/sonarqube-ant-task.jar:dist/lib/httpclient.jar:dist/lib/winrun4j.jar:dist/lib/commons-httpclient.jar:dist/lib/activation.jar:dist/lib/htmlcleaner.jar:dist/lib/commons-codec.jar:dist/lib/junit.jar:dist/lib/swt.jar:dist/lib/stax-api.jar"

COMMON_DEPS="" #=dev-java/commons-httpclient-3.1"
DEPEND="
        >=virtual/jdk-1.8
        ${COMMON_DEPS}"
RDEPEND="
        >=virtual/jre-1.8
        ${COMMON_DEPS}"
        
EANT_DOC_TARGET="doc"

pkg_setup() {
	if use server ; then
		enewuser davmail -1 -1 /dev/null
	fi
}

src_prepare() {
        ewarn "Dependencies are bundled and installed with this ebuild. Ant takes care of them. This should be fixed."
        java-pkg-2_src_prepare
}

src_install() {
        

        for JAR in $( find "${S}"/dist/lib/ -type f -name \*.jar ); do
             java-pkg_newjar "${JAR}" $(basename "${JAR}" | sed 's/-[0-9].*//')".jar" #strip version from dependencies filename. This is what the gentoo dev manual recommands if I understood correctly. This is also useful for EANT_GENTOO_CLASSPATH_EXTRA, because I don't have to update it after a dependency version bump.
        done 
        
         java-pkg_dojar dist/davmail.jar 

        
         java-pkg_dolauncher davmail --jar davmail.jar --main davmail.DavGateway
         
         doicon "${FILESDIR}"/${PN}.png
         
         make_desktop_entry ${PN} "DavMail" /usr/share/pixmaps/${PN}.png "Network"
         
         if use server ; then
            # log file
            touch ${PN}.log
            insinto /var/log
            doins ${PN}.log
            fowners ${PN} /var/log/${PN}.log

            # config files
            insinto /etc
            doins "${FILESDIR}"/${PN}.properties
            newinitd "${FILESDIR}"/${PN}.init ${PN}
            newconfd "${FILESDIR}"/${PN}.conf ${PN}
	    fi
         #java-pkg_register-dependency "${ED}/usr/share/${PN}"/*.jar
         #java-pkg_regjar "${ED}/usr/share/${PN}"/*.jar ${D}
 #       java-pkg_dolauncher GenomeAnalysisTK --jar GenomeAnalysisTK.jar
#        java-pkg_dolauncher AnalyzeCovariates --jar AnalyzeCovariates.jar
}


pkg_postinst() {
	if use server ; then
		elog
		elog "You have chosen to run ${PN} as a system service. You will need to"
		elog "modify /etc/${PN}.properties to serve your needs."
		elog
		elog "You may find it easier to run the ${PN}, configure it via the GUI"
		elog "and copy the resulting ~/.${PN}.properties to /etc. Make sure you"
		elog "set ${PN}.server=true and ${PN}.allowRemote=true."
		elog
	fi
}
